<template>
	<view>
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :circular="true">
			<swiper-item v-for="(item,i) in goodDetail.pics" :key="i">
				<image @click="preview(i)" :src="item.pics_big.replace(/^http:/,'https:')">

				</image>
			</swiper-item>
		</swiper>
		<view class="good-info-box">
			<view class="price">￥{{goodDetail.goodsPrice}}</view>
			<view class="good-info-body">
				<view class="good-name">{{goodDetail.goodsName}}</view>
				<view class="favi">
					<uni-icons type="star" size="30"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">
				<text>运费:
					{{cartList.length}}</text>
			</view>
		</view>

		<rich-text :nodes="goodDetail.introduce"> </rich-text>
		<view class="zhanwei">
			
		</view>
		<view class="good-nav">
			<uni-goods-nav @click="onClick" :fill="true" :options="options" @buttonClick="buttonClick"
				:buttonGroup="buttonGroup"></uni-goods-nav>
		</view>

	</view>

</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'

	export default {
		computed: {
			...mapState('cart', ['cartList']),
			...mapGetters('cart',['total'])
		},
		watch:{
			total:{
				handler(newvalue){
					const res = this.options.find(x=>x.text === '购物车')
					if(res){
						res.info = newvalue
					}
					
				},
				immediate : true
			}
		},
		data() {
			return {
				goodId: 0,
				goodDetail: {},
				options: [{
						icon: 'shop',
						text: '店铺',
					},
					{
						icon: 'cart',
						text: '购物车',
						info: 0
					}
				],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		methods: {
			...mapMutations('cart', ['addToCart']),
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					const goods = {
						goodsId: this.goodDetail.goodsId,
						goodsName: this.goodDetail.goodsName,
						goodsPrice: this.goodDetail.goodsPrice,
						goods_count: 1,
						goodsSmallLogo: this.goodDetail.goodsSmallLogo.replace(/http:\/\//g,'https://'),
						goods_state: true
					}
					this.addToCart(goods)
				}
			},
			onClick(e) {
				console.log(e.content.text)
				if (e.content.text === '购物车') {
					console.log("ok")
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			preview(i) {
				uni.previewImage({
					current: i,
					urls: this.goodDetail.pics.map(s => s.pics_big.replace(/^http:/, 'https:'))
				})
			},
			async getGoodDetails() {
				const {
					data
				} = await uni.$http.get('http://192.168.1.11:8080/get/goods/detail', {
					good_id: this.goodId
				})


				this.goodDetail = data.result
				this.goodDetail.introduce = this.goodDetail.introduce.replace(/<img /g, '<img style="display:block;"')
					.replace(/webp/g, 'jpg').replace(/^http:\/\//g, 'https://')
				console.log(this.goodDetail)
			}

		},

		onLoad(options) {
			this.goodId = options.goodId
			this.getGoodDetails()

		}
	}
</script>

<style lang="scss">
	.zhanwei{
		height: 55px;
		width: 100%;
	}
	.good-nav {
		position: fixed;
		bottom: 0;
		width: 100%;
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.good-info-box {
		padding: 10px;

		.price {
			color: red;
			padding: 10px 0;
			font-size: 18px;
		}

		.good-info-body {
			.good-name {
				font-size: 13px;
				margin-right: 10px;
			}

			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}

			display: flex;
			justify-content: space-between;
		}

		.yf {
			font-size: 12px;
			color: gray;
			margin: 10px 0;
		}

	}
</style>